package com.programs.waw;

import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.util.Log;

public class ReadAliceDatas {

	private ArrayList<AliceData> aliceList;

	public ReadAliceDatas() {
		aliceList = new ArrayList<AliceData>();
	}

	public ArrayList<AliceData> getList() {
		return aliceList;
	}

	public void read(InputStream is, String ssidPartial) {

		try {

			DocumentBuilder dB = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = dB.parse(is);

			NodeList nodeList = doc.getDocumentElement().getChildNodes();

			for (int i = 0; i < nodeList.getLength(); i++) {

				Node node_i = nodeList.item(i);

				if (node_i.getNodeType() == Node.ELEMENT_NODE) {

					Element e = (Element) node_i;
					String ssidNode = e.getNodeName().substring(5);

					if (ssidNode.startsWith(ssidPartial.substring(0, 2))) {

						AliceData aliceData = new AliceData();

						aliceData.setSsid(ssidNode);
						aliceData.setSn(e.getAttribute("sn"));
						aliceData.setK(Double.parseDouble(e.getAttribute("k")));
						aliceData.setQ(Double.parseDouble(e.getAttribute("q")));
						aliceData.setMac(e.getAttribute("mac"));

						// Log.i("aliceData Read", aliceData.toString());

						aliceList.add(aliceData);

					}
				}
			}
		} catch (Exception e) {
			Log.e("exception", e.toString());
		}
	}

}
